from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession

# create async engine
engine = create_async_engine('postgresql+asyncpg://user:password@localhost/mydatabase')

# create async session
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)

# async with session
async with async_session() as session:
    # perform async database operations
    result = await session.execute("SELECT * FROM mytable")
    rows = result.fetchall()


# self.db_url = f"postgresql+psycopg2://{pgc['user']}:{password}@{pgc['host']}:{pgc['port']}/{pgc['db_name']}"
# self.engine = sqlalchemy.create_engine(self.db_url, encoding="UTF-8", echo=False,
#                                        isolation_level="REPEATABLE READ",
#                                        pool_use_lifo=True,
#                                        pool_pre_ping=True,
#                                        pool_size=3,
#                                        pool_recycle=900,
#                                        max_overflow=2)
# self.session = scoped_session(sessionmaker(bind=self.engine))()
